package com.example.algorithm.algorithmService;

import com.example.algorithm.mapper.ControlPowerDayMapper;
import com.example.algorithm.mapper.SvYdNumberMapper;
import com.example.algorithm.pojo.SvYdNumberDO;
import com.example.algorithm.result.ControlPowerDay;
import com.example.algorithm.result.DepartmentControlMark;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.OptionalDouble;

@Service
public class ControlPowerDayService {
    @Autowired
    private ControlPowerDayMapper controlPowerDayMapper;

    @Resource
    private SvYdNumberMapper numberMapper;


    public HashMap<Date, ControlPowerDay>  getControlPowerDay(List<DepartmentControlMark> resultList, Date date, HashMap<Date, ControlPowerDay> resultDayMap) {
        Double initiativeDiscoveryMarkDay = getInitiativeDiscoveryMarkDay(resultList);

        Double reformMarkDay = getReformMarkDay(resultList);

        Double questionReformMarkDay = getQuestionReformMarkDay(resultList);

        Double stockScoreDay = getStockScoreDay(initiativeDiscoveryMarkDay, reformMarkDay, questionReformMarkDay);

        Double passiveDiscoveryMarkDay = getPassiveDiscoveryMarkDay(resultList);

        Double recrudesceMarkDay = getRecrudesceMarkDay(resultList);

        Double seriousProblemIgnoreMarkDay = getSeriousProblemIgnoreMarkDay(resultList);

        Double repeatScoreDay = getRepeatScoreDay(passiveDiscoveryMarkDay, recrudesceMarkDay, seriousProblemIgnoreMarkDay);

        Double controlPowerScoreDay = getControlPowerScoreDay(stockScoreDay, repeatScoreDay);

        Double immediateChangeScoreDay = getImmediateChangeScoreDay(resultList);   //   '即知即改'

        Double reformGoodScoreDay = getReformGoodScoreDay(resultList);        //   '整改到位'

        Double activeDiscoveryScoreDay = getActiveDiscoveryScoreDay(resultList);   //   '主动发现'

        Double drawInferencesScoreDay = getDrawInferencesScoreDay(resultList);    //   '举一反三'

        Double sumFiveScoreDay = getSumFiveScoreDay(immediateChangeScoreDay, reformGoodScoreDay, activeDiscoveryScoreDay, drawInferencesScoreDay);

        ControlPowerDay controlPowerDay=new ControlPowerDay();
        controlPowerDay.setComputeDateDay(date);
        controlPowerDay.setInitiativeDiscoveryMarkDay(initiativeDiscoveryMarkDay);
        controlPowerDay.setReformMarkDay(reformMarkDay);
        controlPowerDay.setQuestionReformMarkDay(questionReformMarkDay);
        controlPowerDay.setStockScoreDay(stockScoreDay);
        controlPowerDay.setPassiveDiscoveryMarkDay(passiveDiscoveryMarkDay);
        controlPowerDay.setRecrudesceMarkDay(recrudesceMarkDay);
        controlPowerDay.setSeriousProblemIgnoreMarkDay(seriousProblemIgnoreMarkDay);
        controlPowerDay.setRepeatScoreDay(repeatScoreDay);
        controlPowerDay.setControlPowerScoreDay(controlPowerScoreDay);
        controlPowerDay.setImmediateChangeScoreDay(immediateChangeScoreDay);
        controlPowerDay.setReformGoodScoreDay(reformGoodScoreDay);
        controlPowerDay.setActiveDiscoveryScoreDay(activeDiscoveryScoreDay);
        controlPowerDay.setDrawInferencesScoreDay(drawInferencesScoreDay);
       // controlPowerDay.setSurfaceControlScoreDay(surfaceControlScoreDay);
        controlPowerDay.setSumFiveScoreDay(sumFiveScoreDay);
        resultDayMap.put(controlPowerDay.getComputeDateDay(),controlPowerDay);
        controlPowerDayMapper.insert(controlPowerDay);


        // TODO c插入新数据 (可能数据有问题)
        SvYdNumberDO svYdNumberDO = new SvYdNumberDO();
        svYdNumberDO.setDate(date);
        svYdNumberDO.setProblemNu(stockScoreDay);
        svYdNumberDO.setRepeatNu(repeatScoreDay);
        svYdNumberDO.setListNu(controlPowerScoreDay);
        svYdNumberDO.setDataValid(1);
        numberMapper.insert(svYdNumberDO);

        return  resultDayMap;
    }

    private Double getSumFiveScoreDay(Double immediateChangeScoreDay, Double reformGoodScoreDay, Double activeDiscoveryScoreDay, Double drawInferencesScoreDay) {

        return immediateChangeScoreDay + reformGoodScoreDay + activeDiscoveryScoreDay + drawInferencesScoreDay ;
    }


    private Double getDrawInferencesScoreDay(List<DepartmentControlMark> resultList) {
        double drawInferencesScoreDay = 0d;
        OptionalDouble average = resultList.stream().mapToDouble(DepartmentControlMark::getDrawInferencesScore).average();
        if (average.isPresent()) {
            drawInferencesScoreDay = average.getAsDouble();
        }
        return drawInferencesScoreDay;

    }

    private Double getActiveDiscoveryScoreDay(List<DepartmentControlMark> resultList) {

        double activeDiscoveryScoreDay = 0d;
        OptionalDouble average = resultList.stream().mapToDouble(DepartmentControlMark::getActiveDiscoveryScore).average();
        if (average.isPresent()) {
            activeDiscoveryScoreDay = average.getAsDouble();
        }
        return activeDiscoveryScoreDay;

    }

    private Double getReformGoodScoreDay(List<DepartmentControlMark> resultList) {
        double reformGoodScoreDay = 0d;
        OptionalDouble average = resultList.stream().mapToDouble(DepartmentControlMark::getReformGoodScore).average();
        if (average.isPresent()) {
            reformGoodScoreDay = average.getAsDouble();
        }
        return reformGoodScoreDay;


    }

    private Double getImmediateChangeScoreDay(List<DepartmentControlMark> resultList) {
        double immediateChangeScoreDay = 0d;
        OptionalDouble average = resultList.stream().mapToDouble(DepartmentControlMark::getImmediateChangeScore).average();
        if (average.isPresent()) {
            immediateChangeScoreDay = average.getAsDouble();
        }
        return immediateChangeScoreDay;

    }

    private Double getControlPowerScoreDay(Double stockScoreDay, Double repeatScoreDay) {
        return stockScoreDay + repeatScoreDay;
    }

    private Double getRepeatScoreDay(Double passiveDiscoveryMarkDay, Double recrudesceMarkDay, Double seriousProblemIgnoreMarkDay) {
        return passiveDiscoveryMarkDay + recrudesceMarkDay + seriousProblemIgnoreMarkDay;
    }

    private Double getSeriousProblemIgnoreMarkDay(List<DepartmentControlMark> resultList) {
        double seriousProblemIgnoreMarkDay = 0d;
        OptionalDouble average = resultList.stream().mapToDouble(DepartmentControlMark::getSeriousProblemIgnoreMark).average();
        if (average.isPresent()) {
            seriousProblemIgnoreMarkDay = average.getAsDouble();
        }
        return seriousProblemIgnoreMarkDay;
    }

    private Double getRecrudesceMarkDay(List<DepartmentControlMark> resultList) {
        double recrudesceMarkDay = 0d;
        OptionalDouble average = resultList.stream().mapToDouble(DepartmentControlMark::getRecrudesceMark).average();
        if (average.isPresent()) {
            recrudesceMarkDay = average.getAsDouble();
        }
        return recrudesceMarkDay;

    }

    private Double getPassiveDiscoveryMarkDay(List<DepartmentControlMark> resultList) {
        double passiveDiscoveryMarkDay = 0d;
        OptionalDouble average = resultList.stream().mapToDouble(DepartmentControlMark::getPassiveDiscoveryMark).average();
        if (average.isPresent()) {
            passiveDiscoveryMarkDay = average.getAsDouble();
        }
        return passiveDiscoveryMarkDay;

    }

    private Double getStockScoreDay(Double initiativeDiscoveryMarkDay, Double reformMarkDay, Double questionReformMarkDay) {
        return initiativeDiscoveryMarkDay + reformMarkDay + questionReformMarkDay;
    }

    private Double getQuestionReformMarkDay(List<DepartmentControlMark> resultList) {
        double questionReformMarkDay = 0d;
        OptionalDouble average = resultList.stream().mapToDouble(DepartmentControlMark::getQuestionReformMark).average();
        if (average.isPresent()) {
            questionReformMarkDay = average.getAsDouble();
        }
        return questionReformMarkDay;
    }

    private Double getReformMarkDay(List<DepartmentControlMark> resultList) {
        double reformMarkDay = 0d;
        OptionalDouble average = resultList.stream().mapToDouble(DepartmentControlMark::getReformMark).average();
        if (average.isPresent()) {
            reformMarkDay = average.getAsDouble();
        }
        return reformMarkDay;
    }

    private Double getInitiativeDiscoveryMarkDay(List<DepartmentControlMark> resultList) {
        double initiativeDiscoveryMarkDay = 0d;

        OptionalDouble average = resultList.stream().mapToDouble(DepartmentControlMark::getInitiativeDiscoveryMark).average();
        if (average.isPresent()) {
            initiativeDiscoveryMarkDay = average.getAsDouble();
        }
        return initiativeDiscoveryMarkDay;
    }

}
